/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年6月4日         luwei
 */

package com.coocaa.x.service.litecontent.api;

import android.os.RemoteException;

import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.utils.LiteURI;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_apptore.RP_New_Apps;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_apptore.RP_New_Apps.AppStatNum;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_hall.RP_ADInfo;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_theme.RP_Boolean;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_theme.RP_WallpaperCategory;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_theme.RP_WallpaperList;
import com.coocaa.x.service.litecontent.data.system.com.coocaa.x.RP_Upgrade;
import com.coocaa.x.service.litecontent.data.system.com.coocaa.x.RP_Upgrade.Upgrade_Item;

import java.util.List;

public class ContentApi {
    public static Upgrade_Item getUpdateItem() {
        LiteURI uri = LiteURI.create("system://com.coocaa.x/upgrade");
        uri.addParams("device_channel", LiteAppApplication.getDeviceChannel());
        String result;
        try {
            result = LiteContentServiceConnecter.getService().call(uri.toString());
            if (result != null && !result.equals("")) {
                RP_Upgrade data = RP_Upgrade.parseJObject(result, RP_Upgrade.class);
                if (data != null && data.error_code == 0 && data.resource != null && data.resource.total_count > 0) {
                    return data.resource.resource_items.get(0);
                }
            }
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static AppStatNum getNewAppsNum() {
        LiteURI uri = LiteURI.create("store://com.coocaa.appstore/newnums");
        String result;
        try {
            result = LiteContentServiceConnecter.getService().call(uri.toString());
            if (result != null && !result.equals("")) {
                RP_New_Apps data = RP_New_Apps.parseJObject(result, RP_New_Apps.class);
                if (data != null && data.error_code == 0 && data.resource != null && data.resource.total_count > 0) {
                    return data.resource.resource_items.get(0);
                }
            }
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static RP_ADInfo.ADInfo_Item getHallAdInfo(String name) {
        try {
            LiteURI uri = LiteURI.create("store://com.coocaa.hall/adinfo");
            uri.addParams("name", name);
            String result = LiteContentServiceConnecter.getService().call(uri.toString());
            if (result != null && !result.equals("")) {
                RP_ADInfo data = RP_ADInfo.parseJObject(result, RP_ADInfo.class);
                if (data != null && data.error_code == 0 && data.resource != null && data.resource.total_count > 0) {
                    return data.resource.resource_items.get(0);

                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<RP_WallpaperCategory.WallPaper_Category_Item> getWallpaperCategory() {
        try {
            LiteURI uri = LiteURI.create("store://com.coocaa.theme/wallpaper/category");
            String result = LiteContentServiceConnecter.getService().call(uri.toString());
            if (result != null && !result.equals("")) {
                RP_WallpaperCategory data = RP_WallpaperCategory.parseJObject(result, RP_WallpaperCategory.class);
                if (data != null && data.error_code == 0 && data.resource != null && data.resource.total_count > 0) {
                    return data.resource.resource_items;
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static List<RP_WallpaperList.WallPaper_Item> getWallpaperList(String id, int index, int count) {
        try {
            LiteURI uri = LiteURI.create("store://com.coocaa.theme/wallpaper/list");
            uri.addParams("id", id);
            uri.addParams("index", String.valueOf(index));
            uri.addParams("count", String.valueOf(count));
            String result = LiteContentServiceConnecter.getService().call(uri.toString());
            if (result != null && !result.equals("")) {
                RP_WallpaperList data = RP_WallpaperList.parseJObject(result, RP_WallpaperList.class);
                if (data != null && data.error_code == 0 && data.resource != null && data.resource.total_count > 0) {
                    return data.resource.resource_items;
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean collectWallpaper(String json) {
        LogUtils.d("wallpaper", json);
        try {
            LiteURI uri = LiteURI.create("store://com.coocaa.theme/wallpaper/collection");
            uri.addParams("action", "add");
            uri.addParams("param", json);
            LogUtils.d("wallpaper", uri.toString());
            String result = LiteContentServiceConnecter.getService().call(uri.toString());
            if (result != null && !result.equals("")) {
                RP_Boolean data = RP_Boolean.parseJObject(result, RP_Boolean.class);
                if (data != null && data.error_code == 0) {
                    return data.resource;
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean unCollectWallpaper(String json) {
        try {
            LiteURI uri = LiteURI.create("store://com.coocaa.theme/wallpaper/collection");
            uri.addParams("action", "delete");
            uri.addParams("param", json);
            String result = LiteContentServiceConnecter.getService().call(uri.toString());
            if (result != null && !result.equals("")) {
                RP_Boolean data = RP_Boolean.parseJObject(result, RP_Boolean.class);
                if (data != null && data.error_code == 0) {
                    return data.resource;
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setWallpaper(String json) {
        try {
            LiteURI uri = LiteURI.create("store://com.coocaa.theme/wallpaper/default");
            uri.addParams("action", "set");
            uri.addParams("param", json);
            String result = LiteContentServiceConnecter.getService().call(uri.toString());
            if (result != null && !result.equals("")) {
                RP_Boolean data = RP_Boolean.parseJObject(result, RP_Boolean.class);
                if (data != null && data.error_code == 0) {
                    return data.resource;
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static RP_WallpaperList.WallPaper_Item getCurrentWallpaper() {
        try {
            LiteURI uri = LiteURI.create("store://com.coocaa.theme/wallpaper/default");
            uri.addParams("action", "get");
            String result = LiteContentServiceConnecter.getService().call(uri.toString());
            if (result != null && !result.equals("")) {
                RP_WallpaperList data = RP_WallpaperList.parseJObject(result, RP_WallpaperList.class);
                if (data != null && data.error_code == 0
                        && data.resource != null
                        && data.resource.total_count > 0) {
                    return data.resource.resource_items.get(0);
                }
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

}
